<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>document</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <!--
        1.vue会监视data中所有层次数据
        2.监测对象数据1）创建vue实例时data的数据，2）Vue.set/vm.$set后天增加的数据
        3.监测数组数据1）通过7个原地修改数组的方法 2）通过不修改原数组的方法处理后，替换原数组
        4.Vue.set/vm.$set不能给data或vm添加一级属性
    -->
    <div id="root">
        <h1>学生信息</h1>

        <button @click="student.age++">年龄+1</button>
        <button @click="addSex">添加性别属性，默认男</button>
        <button @click="student.sex='未知'">修改性别</button>
        <button @click="addFriend">在开头添加一个朋友</button>
        <button @click="updateFriend">修改第一个朋友名字为张三</button>
        <button @click="addHobby">添加一个爱好</button>
        <button @click="updateHobby">修改第一个爱好名字为开车</button>
        <button @click="filterHobby">过滤爱好中的抽烟</button>

        <h2>姓名：{{student.name}}</h2>
        <h2>年龄：{{student.age}}</h2>
        <h2 v-if="student.sex">性别：{{student.sex}}</h2>
        <h2>爱好</h2>
        <ul>
            <li v-for="(h,index) in student.hobby" :key="index">
                {{h}}
            </li>
        </ul>
       
        <h2>朋友们</h2>
        <ul>
            <li v-for="(f,index) in student.friends" :key="index">
                {{f.name}}--{{f.age}}
            </li>
        </ul>
       
    </div>
    <script>
        Vue.config.productionTip = false
        let vm = new Vue({
            el: "#root", 
            data: { 
                student: {
                    name: 'tom',
                    age: 24,
                    hobby:[
                        '抽烟','喝酒','烫头'
                    ],
                    friends: [
                        {
                            name: 'jerry',
                            age: 35,
                        },
                        {
                            name: 'tony',
                            age: 30,
                        }
                    ]
                }
            },
            methods: {
                addSex(){
                   this.$set(this.student, 'sex', '男')
                },
                addFriend() {
                   this.student.friends.unshift({
                            name: 'van',
                            age: 30,
                        })
                },
                updateFriend() {
                    this.student.friends[0].name = '张三'
                },
                addHobby() {
                    this.student.hobby.push('学习')
                },
                updateHobby() {
                    //错误写法
                    // this.student.hobby[0] = '开车'
                    //正确写法1
                    // this.student.hobby.splice(0, 1, '开车')
                    //正确写法2
                    Vue.set(this.student.hobby, 0, '开车')
                },
                filterHobby() {
                    this.student.hobby = this.student.hobby.filter((h)=>{
                        return h != '抽烟'
                    })
                }
            },
        })
    </script>
</body>
</html>